unit TransactionDM;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  MainDM, ZAbstractDataset;

type

  { TTransactionDataModule }

  TTransactionDataModule = class(TMainDataModule)
  protected
    /// Procedimento que deleta os detalhes
    procedure DeleteDetail(); virtual; abstract;
  public
    /// Procedimento que deleta o mestre e os detalhes relacionados
    procedure Delete(); virtual;
    /// Inicia transação para alteração dos detalhes
    procedure EditDetail(); virtual;
    /// Dá commit na transação iniciada
    procedure PostDetail(); virtual;
    /// Dá Rollback na transação iniciada
    procedure CancelDetail(); virtual;
  end;

var
  TransactionDataModule: TTransactionDataModule;

implementation

procedure TTransactionDataModule.Delete;
begin
  TZAbstractDataset(MainDataSet).Connection.StartTransaction();
  DeleteDetail();
  inherited Delete();
  TZAbstractDataset(MainDataSet).Connection.Commit();
end;

procedure TTransactionDataModule.EditDetail;
begin
  TZAbstractDataset(MainDataSet).Connection.StartTransaction();
end;

procedure TTransactionDataModule.PostDetail;
begin
  TZAbstractDataset(MainDataSet).Connection.Commit();
end;

procedure TTransactionDataModule.CancelDetail;
begin
  TZAbstractDataset(MainDataSet).Connection.Rollback();
end;

initialization
  {$I transactiondm.lrs}

end.

